'''

Case Type： 数据库系统
Case Name： 数据库停止时，清空配置文件内容
Create At:  2020/09/07
@zou_jialiang0505328126
Descption: 1.单机/一主一备
           2.数据库已停止


history：
created： 2020/09/07  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
import _thread
import queue
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *

logger = Logger()
common = Common()
commonsh = CommonSH()


class StopImmediate(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    BUILD_SUCCESS_MSG = Constant.BUILD_SUCCESS_MSG
    BUILD_FAIL_MSG = Constant.BUILD_FAIL_MSG_OPEN
    TPCC_PATH = macro.TPCC_PATH
    commonsh_user = CommonSH('PrimaryDbUser')
    conffile = macro.DB_INSTANCE_PATH + '/' + macro.DB_PG_CONFIG_NAME
    conffilebak = macro.DB_INSTANCE_PATH + '/' + macro.DB_PG_CONFIG_NAME + '.baktest'
    basefile = macro.DB_INSTANCE_PATH + '/base'
    basefilebak = basefile + '.baktest'
    BUILD_FAIL_MSG_OPEN = Constant.BUILD_FAIL_MSG_OPEN
    DROP_TABLE_SUCCESS = Constant.DROP_TABLE_SUCCESS
    CREATE_TABLE_SUCCESS = Constant.CREATE_TABLE_SUCCESS
    STOP_NODE_SUCCESS_MSG = Constant.STOP_NODE_SUCCESS_MSG
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info('------------Opengauss_Reliability_Dbsys_Case117.py start------------')
        logger.info("------------------stop pri----------------------")
        res = self.commonsh_user.stopDbCluster()
        self.assertTrue(res)

    def test_stop_immediate(self):
        logger.info("------------------clear conf file----------------------")
        deleteCmd = f"cp -R {self.conffile} {self.conffilebak}"
        logger.info(deleteCmd)
        self.dbPrimaryUserNode.sh(deleteCmd)
        deleteCmd = f">{self.conffile}"
        logger.info(deleteCmd)
        self.dbPrimaryUserNode.sh(deleteCmd)
        logger.info(self.dbPrimaryUserNode.sh(f'cat {self.conffile}').result())

        logger.info("------------------start pri----------------------")
        res = self.commonsh_user.startDbCluster()
        self.assertFalse(res)

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        logger.info("------------------------recover conf file------------------")
        recoverCmd = f'rm -f {self.conffile}'
        logger.info(recoverCmd)
        self.dbPrimaryUserNode.sh(recoverCmd)
        recoverCmd = f"cp -R {self.conffilebak} {self.conffile}; rm -rf {self.conffilebak}"
        logger.info(recoverCmd)
        self.dbPrimaryUserNode.sh(recoverCmd)

        logger.info("------------------start pri----------------------")
        res = self.commonsh_user.stopDbCluster()
        self.assertTrue(res)
        res = self.commonsh_user.startDbCluster()
        self.assertTrue(res)
